package com.glsc.ngateway.oaflow.schedule;

import com.glsc.ngateway.common.api.oaflow.dto.xirtrade.TOaFlowXirTradeBaseInfoDto;
import com.glsc.ngateway.oaflow.service.OaSyncService;
import com.glsc.ngateway.oaflow.service.OracleJdbcTemplate;
import com.glsc.ngateway.oaflow.service.xirtrade.TOaFlowXirTradeService;
import com.xxl.job.core.context.XxlJobHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

import static com.glsc.ngateway.oaflow.utils.Constant.PRIMARY_NODE;

@Component
public class XxlJob {

    @Autowired
    private OaSyncService oaSyncService;

    @Autowired
    private TOaFlowXirTradeService TOaFlowXirTradeInfoService;

    @Resource
    private OracleJdbcTemplate oracleJdbcTemplate;

    /**
     * 弃用
     */
    @com.xxl.job.core.handler.annotation.XxlJob("XxlJob_checkSysnLog")
    public void checkSysnLogDol() {
        oaSyncService.checkSysnLog();
    }

    @com.xxl.job.core.handler.annotation.XxlJob("XxlJob_syncXirTrade")
    public void syncXirTradeDol(String params) {
        params = XxlJobHelper.getJobParam();
        String[] paramArray = params.split(",");
        String type = paramArray[0];
        boolean unPushFlow =false;
        String date = "";
        if (paramArray.length >= 2){
            String push = paramArray[1];
            if ("true".equals(push)){
                unPushFlow= true;
            }
        }
        if (paramArray.length >= 3){
            date = paramArray[2];
        }
        date = StringUtils.isBlank(date) ? LocalDate.now().toString() : date;
        List<TOaFlowXirTradeBaseInfoDto> res = TOaFlowXirTradeInfoService.syncXirTrade(date, unPushFlow, type);
    }

    @com.xxl.job.core.handler.annotation.XxlJob("XxlJob_ResetDataSource")
    public void ResetDataSource() {
        //将说数据源恢复到主节点
        if (!StringUtils.equals(oracleJdbcTemplate.getCurrentJdbc(),PRIMARY_NODE)){
            oracleJdbcTemplate.switchJdbc();
        }
    }
}
